<template>
  <el-dialog
    title="二级分类"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="60%">
    <el-button type="primary" @click="outerSubordinateAddOrUpdateHandle()">新增</el-button>
    <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="ID"
          width="80">
        </el-table-column>
        <el-table-column
          prop="outerTitle"
          label="分类名">
        </el-table-column>
        <el-table-column
          prop="outerUrl"
          label="地址">
        </el-table-column>
        <el-table-column
          prop="isEnable"
          header-align="center"
          align="center"
          label="显示">
          <template slot-scope="scope">
            <el-switch
            style="margin-left: 10px;margin-bottom: 5px;"
            v-model="scope.row.isEnable === true"
            @change="batch(scope.row.id, scope.row.isEnable === true ? 0 : 1)">
          </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="outerSubordinateAddOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <outer-subordinate-add-or-update v-if="outerSubordinateAddOrUpdateVisible" ref="outerSubordinateAddOrUpdate" @refreshDataList="init"></outer-subordinate-add-or-update>
  </el-dialog>
</template>

<script>
  import OuterSubordinateAddOrUpdate from './outer-subordinate-add-or-update'
  export default {
    data () {
      return {
        visible: false,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataList: [],
        dataForm: {
          id: 0,
          sequence: '',
          outerTitle: '',
          outerUrl: ''
        },
        dataListLoading: false,
        outerSubordinateAddOrUpdateVisible: false,
        dataRule: {
          outerTitle: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      OuterSubordinateAddOrUpdate
    },
    methods: {
      init: function (id) {
        this.dataForm.id = id
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$http({
          url: this.$http.adornUrl('/outer/queryOuterChainSubordinateList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.id
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.init(this.dataForm.id)
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.init(this.dataForm.id)
      },
      outerSubordinateAddOrUpdateHandle (id) {
        this.outerSubordinateAddOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.outerSubordinateAddOrUpdate.init(id, this.dataForm.id)
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/outer/${!this.dataForm.id ? 'addOuterChain' : 'modifyOuterChainById'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'outerTitle': this.dataForm.className,
                'seq': this.dataForm.sequence,
                'outerUrl': this.dataList.outerUrl
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      batch (id, display) {
        this.$http({
          url: this.$http.adornUrl(`/outer/modifyIsEnableById/${id}/${display}`),
          method: 'post',
          data: this.$http.adornData()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.init(this.dataForm.id)
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/outer/removeOuterChain'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.init(this.dataForm.id)
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
